package com.aier.user.entity.userapp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;



import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

public class ModuleRowMapper implements RowMapper<Module>{
    private int type;

    public static final int Module_AND_STATE = 1;

    public ModuleRowMapper() {
        
    }

    public ModuleRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Module mapRow(ResultSet rs, int rowNum) throws SQLException {
        Module cd = new Module();
        switch (type) {
            case Module_AND_STATE:
            	cd = parseModule(new Module(), rs);
            	int num = rs.getInt("num");
            	if(cd.getState()==0 && num>0){
                    cd.setState((short) 1);
            	}
                break;
            default:
            	cd = parseModule(new Module(), rs);
        }  
        return cd;
    }

    public static Module parseModule(Module m, ResultSet rs)
        throws SQLException {
        return parseModule(m, rs, null);
    }

    public static Module parseModule(Module m, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            m.setMid(rs.getLong(su + "mid"));
        } catch (SQLException e) {
            m.setMid(null);
        }
        try {
            m.setMname(rs.getString(su + "mname"));
        } catch (SQLException e) {
            m.setMname(null);
        }
        try {
            m.setState(rs.getShort(su + "state"));
        } catch (SQLException e) {
            m.setState(null);
        }

        
        return m;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            Module m) {
        par.put("mid", m.getMid());
        par.put("mname", m.getMname());
        par.put("state", m.getState());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            Module m) {
        args.put("mid", m.getMid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Module m) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(m),
                        new String[] { "mid" });
                parsePS(ps, m);
                return ps;
            }

        };
    }

    private static String parseSQL(Module m) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_module(");
        int count = 0;

        if (null != m.getMid()) {
            sql.append("mid,");
            count++;
        }
        if (null != m.getMname()) {
            sql.append("mname,");
            count++;
        }
        if (null != m.getState()) {
            sql.append("state,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Module m)
        throws SQLException {
        int count = 1;
        if (null != m.getMid()) {
            ps.setLong(count++, m.getMid());
        }
        if (null != m.getMname()) {
            ps.setString(count++, m.getMname());
        }  
        if (null != m.getState()) {
            ps.setInt(count++, m.getState());
        } 
    }

}
